package crp.infra.ipinfo;

import com.google.common.base.Strings;
import crp.core.analysis.domain.models.IpInfo;
import crp.core.analysis.domain.services.IpInfoLoader;
import crp.core.shared.cache.CacheNames;
import io.quarkus.arc.DefaultBean;
import io.quarkus.cache.CacheResult;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@DefaultBean
@ApplicationScoped
public class IpInfoLoaderImpl implements IpInfoLoader {
    private final IpInfoRestClient restClient;

    @Inject
    public IpInfoLoaderImpl(@RestClient IpInfoRestClient restClient) {
        this.restClient = restClient;
    }

    @Override
    @CacheResult(cacheName = CacheNames.INFRA_IP_INFO)
    public IpInfo load(String ip) {
        if (Strings.isNullOrEmpty(ip)) {
            return IpInfo.builder().build();
        }
        return restClient.get(ip).ofIpInfo();
    }
}
